package com.uchedao.vehicledetection.adapter;

import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Pager适配器
 */
public class PagerViewAdapter extends PagerAdapter {
    List<View> list = new ArrayList<View>();
//    private boolean index = false;

    public PagerViewAdapter(List<View> list) {
        this.list = list;
    }


    public void updataData(View view) {
//        index = !index;
//        if (index) {//添加在首个
            list.remove(list.size() - 1);
            list.add(0, view);
//        } else {//添加在末尾
//            list.remove(list.size() - 1);
//            list.add(view);
//        }
//        return index;
//		notifyDataSetChanged();
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ViewPager pViewPager = ((ViewPager) container);
        pViewPager.removeView(list.get(position));
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public Object instantiateItem(View arg0, int arg1) {

//		ViewPager pViewPager = ((ViewPager) arg0);
//		if(list.get(arg1)==null)
//			return null;
//		pViewPager.addView(list.get(arg1),0);
//		return list.get(arg1);

        list.get(arg1).setTag(arg1);
        ((ViewPager) arg0).addView(list.get(arg1));


        return list.get(arg1);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {

    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
    }

}
